<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <meta name="author" content="隐星魂 (Roy.Sun)">
        
        <link rel="shortcut icon" href="../../../img/favicon.ico">
        <title>3.1.线程库 - 玄武操作系统</title>
        <link href="../../../css/bootstrap.min.css" rel="stylesheet">
        <link href="../../../css/font-awesome.min.css" rel="stylesheet">
        <link href="../../../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.3/styles/color-brewer.min.css">

        <script src="../../../js/jquery-1.10.2.min.js" defer></script>
        <script src="../../../js/bootstrap.min.js" defer></script>
        <script src="https://cdn.staticfile.org/highlight.js/10.0.3/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>
        <div class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
            <div class="container">
                <a class="navbar-brand" href="../../..">玄武操作系统</a>
                <!-- Expander button -->
                <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbar-collapse">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <!-- Expanded navigation -->
                <div id="navbar-collapse" class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li class="navitem">
                                <a href="../../.." class="nav-link">主页</a>
                            </li>
                            <li class="navitem">
                                <a href="../../../License/" class="nav-link">开源协议</a>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">快速开发指南 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">STM32</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../QuickGuide/stm32/01-Envsetup-Guide/" class="dropdown-item">01-开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/02-atkh743/" class="dropdown-item">02-正点原子阿波罗H743开发板上手指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/03-fk429m/" class="dropdown-item">03-反客F429核心板上手指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/04-atkf103core/" class="dropdown-item">04-正点原子F103核心板上手指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/05-atkf407core/" class="dropdown-item">05-正点原子F407核心板上手指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/06-WeActH750/" class="dropdown-item">06-微行电子H750开发板上手指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/stm32/07-Project-From-Scratch-Guide/" class="dropdown-item">07-自定义工程配置指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">GD32</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../QuickGuide/gd32/01-Envsetup-Guide/" class="dropdown-item">01-开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/gd32/02-Project-Setup-Guide/" class="dropdown-item">02-工程配置指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">GD32V</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../QuickGuide/gd32v/01-Envsetup-Guide/" class="dropdown-item">01-开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/gd32v/02-Project-Setup-Guide/" class="dropdown-item">02-工程配置指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">NXP S32K</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../QuickGuide/s32k/01-Envsetup-Guide/" class="dropdown-item">01-开发环境搭建指南</a>
</li>
            
<li>
    <a href="../../../QuickGuide/s32k/02-Project-Setup-Guide/" class="dropdown-item">02-工程配置指南</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">玄武内核模块</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../QuickGuide/xwko/01-XuanWuKO-Guide/" class="dropdown-item">01-上手指南</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">应用手册 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../../UserManual/00-Index/" class="dropdown-item">0.目录</a>
</li>
                                    
<li>
    <a href="../../../UserManual/01-Brief/" class="dropdown-item">1.简介</a>
</li>
                                    
<li>
    <a href="../../../UserManual/02-Basic-System-Architecture/" class="dropdown-item">2.系统基本架构</a>
</li>
                                    
<li>
    <a href="../../../UserManual/03-Build-System/" class="dropdown-item">3.构建系统</a>
</li>
                                    
<li>
    <a href="../../../UserManual/04-Boot-Flow/" class="dropdown-item">4.启动流程</a>
</li>
                                    
<li>
    <a href="../../../UserManual/05-IRQ/" class="dropdown-item">5.中断控制</a>
</li>
                                    
<li>
    <a href="../../../UserManual/06-SKD/" class="dropdown-item">6.调度器</a>
</li>
                                    
<li>
    <a href="../../../UserManual/07-Thread/" class="dropdown-item">7.线程</a>
</li>
                                    
<li>
    <a href="../../../UserManual/08-SWT/" class="dropdown-item">8.软件定时器</a>
</li>
                                    
<li>
    <a href="../../../UserManual/09-PM/" class="dropdown-item">9.电源管理</a>
</li>
                                    
<li>
    <a href="../../../UserManual/10-Lock/" class="dropdown-item">10.锁</a>
</li>
                                    
<li>
    <a href="../../../UserManual/11-Sync/" class="dropdown-item">11.同步</a>
</li>
                                    
<li>
    <a href="../../../UserManual/12-MM/" class="dropdown-item">12.内存管理</a>
</li>
                                    
<li>
    <a href="../../../UserManual/13-C-Lib/" class="dropdown-item">13.玄武C库</a>
</li>
                                </ul>
                            </li>
                            <li class="dropdown active">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">Lua手册 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../00-Index/" class="dropdown-item">0.目录</a>
</li>
                                    
<li>
    <a href="../../01-Brief/" class="dropdown-item">1.简介</a>
</li>
                                    
<li>
    <a href="../../02-XWXT/" class="dropdown-item">2.全局导出表</a>
</li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">3.XWOS内核</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="./" class="dropdown-item active">3.1.线程库</a>
</li>
            
<li>
    <a href="../02-Spinlock/" class="dropdown-item">3.2.自旋锁库</a>
</li>
            
<li>
    <a href="../03-Seqlock/" class="dropdown-item">3.3.顺序锁库</a>
</li>
            
<li>
    <a href="../04-Mutex/" class="dropdown-item">3.4.互斥锁库</a>
</li>
            
<li>
    <a href="../05-Semaphore/" class="dropdown-item">3.5.信号量库</a>
</li>
            
<li>
    <a href="../06-Condition/" class="dropdown-item">3.6.条件量库</a>
</li>
            
<li>
    <a href="../07-Flag/" class="dropdown-item">3.7.事件标志库</a>
</li>
            
<li>
    <a href="../08-Barrier/" class="dropdown-item">3.8.线程栅栏库</a>
</li>
            
<li>
    <a href="../09-Selector/" class="dropdown-item">3.9.信号选择器库</a>
</li>
            
<li>
    <a href="../10-PM/" class="dropdown-item">3.10.电源管理</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">4.玄武Lua函数库</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../04-XWLIB/01-Bitmap/" class="dropdown-item">4.1.位图</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">5.设备栈</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../05-XWDS/01-Soc/" class="dropdown-item">5.1.SOC</a>
</li>
            
<li>
    <a href="../../05-XWDS/02-GPIO/" class="dropdown-item">5.2.GPIO</a>
</li>
            
<li>
    <a href="../../05-XWDS/03-UART/" class="dropdown-item">5.3.UART</a>
</li>
            
<li>
    <a href="../../05-XWDS/04-SPI-Master/" class="dropdown-item">5.4.SPI Master</a>
</li>
            
<li>
    <a href="../../05-XWDS/05-IIC-Master/" class="dropdown-item">5.5.IIC Master</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">移植笔记 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../../PortingNote/00-Index/" class="dropdown-item">目录</a>
</li>
                                    
<li>
    <a href="../../../PortingNote/01-Porting-Note/" class="dropdown-item">移植笔记</a>
</li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">ARM-Cortex-M</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../PortingNote/02-ARM-Cortex-M/01-ARM-Cortex-M-Porting-Note/" class="dropdown-item">架构移植笔记</a>
</li>
            
<li>
    <a href="../../../PortingNote/02-ARM-Cortex-M/02-STM32-Porting-Note/" class="dropdown-item">STM32</a>
</li>
    </ul>
  </li>
                                    
  <li class="dropdown-submenu">
    <a href="#" class="dropdown-item">RISC-V</a>
    <ul class="dropdown-menu">
            
<li>
    <a href="../../../PortingNote/03-RISC-V/01-GD32V-Porting-Note/" class="dropdown-item">GD32V</a>
</li>
    </ul>
  </li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">规范 <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li>
    <a href="../../../Specification/00-Index/" class="dropdown-item">目录</a>
</li>
                                    
<li>
    <a href="../../../Specification/01-Git-Commit-Specification/" class="dropdown-item">git-commit规范</a>
</li>
                                    
<li>
    <a href="../../../Specification/02-Version-Branch-Specification/" class="dropdown-item">版本分支规范</a>
</li>
                                </ul>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav ml-auto">
                        <li class="nav-item">
                            <a href="#" class="nav-link" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> 搜索
                            </a>
                        </li>
                            <li class="nav-item">
                                <a rel="prev" href="../../02-XWXT/" class="nav-link">
                                    <i class="fa fa-arrow-left"></i> 上一篇
                                </a>
                            </li>
                            <li class="nav-item">
                                <a rel="next" href="../02-Spinlock/" class="nav-link">
                                    下一篇 <i class="fa fa-arrow-right"></i>
                                </a>
                            </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
            <div class="row">
                    <div class="col-md-3"><div class="navbar-light navbar-expand-md bs-sidebar hidden-print affix" role="complementary">
    <div class="navbar-header">
        <button type="button" class="navbar-toggler collapsed" data-toggle="collapse" data-target="#toc-collapse" title="Table of Contents">
            <span class="fa fa-angle-down"></span>
        </button>
    </div>

    
    <div id="toc-collapse" class="navbar-collapse collapse card bg-secondary">
        <ul class="nav flex-column">
            
            <li class="nav-item" data-level="1"><a href="#31" class="nav-link">3.1.调度器与线程库</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            
            <li class="nav-item" data-level="1"><a href="#311" class="nav-link">3.1.1.描述</a>
              <ul class="nav flex-column">
            <li class="nav-item" data-level="2"><a href="#_1" class="nav-link">线程对象</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#_2" class="nav-link">线程对象强指针</a>
              <ul class="nav flex-column">
              </ul>
            </li>
              </ul>
            </li>
            
            <li class="nav-item" data-level="1"><a href="#312lua-api" class="nav-link">3.1.2.Lua API参考</a>
              <ul class="nav flex-column">
            <li class="nav-item" data-level="2"><a href="#xwosskd" class="nav-link">包：xwos.skd</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#xwosthd" class="nav-link">包：xwos.thd</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#xwoscthd" class="nav-link">包：xwos.cthd</a>
              <ul class="nav flex-column">
              </ul>
            </li>
            <li class="nav-item" data-level="2"><a href="#thdsp" class="nav-link">类：thdsp</a>
              <ul class="nav flex-column">
              </ul>
            </li>
              </ul>
            </li>
        </ul>
    </div>
</div></div>
                    <div class="col-md-9" role="main">

<h1 id="31">3.1.调度器与线程库</h1>
<h1 id="311">3.1.1.描述</h1>
<h2 id="_1">线程对象</h2>
<p>线程对象是由Lua虚拟机调用玄武OS内核API创建的，Lua虚拟机通过玄武OS内核对象的
引用计数间接管理线程对象。</p>
<p>在Lua语言中，通过<strong>线程对象强指针</strong>来表示线程对象。</p>
<h2 id="_2">线程对象强指针</h2>
<p>在Lua语言中，<strong>线程对象强指针</strong>是一种userdata，由C语言实现，等价于玄武OS内核中
的<strong>线程对象描述符</strong>。</p>
<p>Lua虚拟机每创建一个<strong>线程对象强指针</strong>，都会增加相关联线程的引用计数，
Lua虚拟机每回收一个<strong>线程对象强指针</strong>，都会减少相关联线程的引用计数。</p>
<h1 id="312lua-api">3.1.2.Lua API参考</h1>
<h2 id="xwosskd"><strong>包：xwos.skd</strong></h2>
<h4 id="xwosskdtt">xwos.skd.tt()</h4>
<ul>
<li>简介：获取当前CPU调度器的系统滴答时间</li>
<li>返回值：(number)系统时间</li>
<li>示例：</li>
</ul>
<pre><code class="lua">xwos.skd.tt()
</code></pre>

<ul>
<li>示例输出：</li>
</ul>
<pre><code class="lua"></code></pre>

<h4 id="xwosskdtc">xwos.skd.tc()</h4>
<ul>
<li>简介：获取当前CPU调度器的系统滴答计数</li>
<li>返回值：(number)滴答计数</li>
<li>示例：</li>
</ul>
<pre><code class="lua">xwos.skd.tc()
</code></pre>

<ul>
<li>示例输出：</li>
</ul>
<pre><code class="lua"></code></pre>

<h4 id="xwosskdts">xwos.skd.ts()</h4>
<ul>
<li>简介：获取当前CPU调度器的系统时间戳</li>
<li>返回值：(number)系统时间</li>
<li>示例：</li>
</ul>
<pre><code class="lua">xwos.skd.ts()
</code></pre>

<ul>
<li>示例输出：</li>
</ul>
<pre><code class="lua"></code></pre>

<h2 id="xwosthd"><strong>包：xwos.thd</strong></h2>
<h4 id="xwosthddofilefile">xwos.thd.dofile(file)</h4>
<ul>
<li>简介：建立一个新的线程，并在新线程中执行脚本文件</li>
<li>参数file：(string)文件路径</li>
<li>返回值：(userdata)新的线程对象的强引用指针</li>
<li>示例：</li>
</ul>
<pre><code class="lua">childthdsp = xwos.thd.dofile(&quot;sd:/hw.lua&quot;)
childthdsp:stop()
</code></pre>

<ul>
<li>示例输出：</li>
</ul>
<pre><code class="lua"></code></pre>

<h4 id="xwosthddostringstr">xwos.thd.dostring(str)</h4>
<ul>
<li>简介：建立一个新的线程，并在新线程中执行字符串表示的脚本</li>
<li>参数str：(string)脚本</li>
<li>返回值：(userdata)新的线程对象的强引用指针</li>
<li>示例：</li>
</ul>
<pre><code class="lua">script = [[
mythdsp = xwos.cthd.sp()
print(&quot;Thread strong pointer:&quot;, mythdsp)
]]
strthdsp = xwos.thd.dostring(script)
strthdsp
strthdsp:stop()
</code></pre>

<ul>
<li>示例输出：</li>
</ul>
<pre><code class="lua"></code></pre>

<h4 id="xwosthdcallfunc">xwos.thd.call(func)</h4>
<ul>
<li>简介：建立一个新的线程，并在新线程中执行函数</li>
<li>参数func：(function)Lua函数</li>
<li>返回值：(userdata)新的线程对象的强引用指针</li>
<li>示例：</li>
</ul>
<pre><code class="lua">function childthd_main ()
  mythdsp = xwos.cthd.sp()
  print(&quot;Thread strong pointer:&quot;, mythdsp)
end
cthdsp = xwos.thd.call(childthd_main)
cthdsp
cthdsp:stop()
</code></pre>

<ul>
<li>示例输出：</li>
</ul>
<pre><code class="lua"></code></pre>

<h2 id="xwoscthd"><strong>包：xwos.cthd</strong></h2>
<h4 id="xwoscthdsp">xwos.cthd.sp()</h4>
<ul>
<li>简介：返回当前线程的<strong>线程对象强指针类</strong>对象</li>
<li>返回值：(userdata)当前线程对象的强引用指针</li>
<li>示例：</li>
</ul>
<pre><code class="lua">mythdsp = xwos.cthd.sp()
</code></pre>

<h4 id="xwoscthdsleeptime">xwos.cthd.sleep(time)</h4>
<ul>
<li>简介：调用此函数的线程睡眠一段时间</li>
<li>参数time：(number, 输入)需要睡眠的时间，单位：纳秒</li>
<li>返回值：(number)错误码<ul>
<li>错误码<ul>
<li>0：睡眠成功</li>
<li>&lt;0：睡眠失败或中断，返回值为Unix错误码</li>
</ul>
</li>
</ul>
</li>
<li>示例：</li>
</ul>
<pre><code class="lua">rc = xwos.cthd.sleep(1000000000) -- 睡眠1s
</code></pre>

<h4 id="xwoscthdsleepfromorigin-inc">xwos.cthd.sleepFrom(origin, inc)</h4>
<ul>
<li>简介：调用此函数的线程从一个时间起点睡眠到另一个时间点</li>
<li>参数origin：(number, 输入)时间起点，单位：纳秒</li>
<li>参数inc：(number, 输入)期望被唤醒的时间相对于起点的增量，单位：纳秒</li>
<li>返回值：{(number)错误码, (number)时间起点}<ul>
<li>错误码<ul>
<li>0：睡眠成功</li>
<li>&lt;0：睡眠失败或中断，返回值为Unix错误码</li>
</ul>
</li>
<li>时间起点：新的时间起点</li>
</ul>
</li>
<li>示例：</li>
</ul>
<pre><code class="lua">origin = xwos.skd.tt()
cnt = 3
repeat
  rc, origin = xwos.cthd.sleepFrom(origin, 1000000000) -- 睡眠1s
  cnt = cnt - 1
until (cnt == 0) -- 重复3次
</code></pre>

<ul>
<li>示例输出：</li>
</ul>
<pre><code class="lua"></code></pre>

<h4 id="xwoscthdyield">xwos.cthd.yield()</h4>
<ul>
<li>简介：通知调度器重新选择线程</li>
<li>示例：</li>
</ul>
<pre><code class="lua">xwos.cthd.yield()
</code></pre>

<h4 id="xwoscthdexitrc">xwos.cthd.exit(rc)</h4>
<ul>
<li>简介：调用此函数的线程退出</li>
<li>参数rc：(number, 输入)线程退出时的返回值</li>
<li>示例：</li>
</ul>
<pre><code class="lua">xwos.cthd.exit(0) -- 线程退出时抛出返回值0
</code></pre>

<h4 id="xwoscthdshldfrz">xwos.cthd.shldFrz()</h4>
<ul>
<li>简介：判断当前线程是否可被冻结</li>
<li>返回值：(boolean)是否可冻结<ul>
<li>true：可被冻结</li>
<li>false：不可被冻结</li>
</ul>
</li>
</ul>
<h4 id="xwoscthdshldstop">xwos.cthd.shldStop()</h4>
<ul>
<li>简介：判断当前线程是否可以退出</li>
<li>返回值：(boolean)是否可退出<ul>
<li>true：可退出</li>
<li>false：不可退出</li>
</ul>
</li>
<li>示例：</li>
</ul>
<pre><code class="lua">-- Lua 线程循环的写法
repeat
  shldStop = xwos.cthd.shldStop()
  threadLoop()
until (shldStop)
</code></pre>

<h4 id="xwoscthdfrzshldstop">xwos.cthd.frzShldStop()</h4>
<ul>
<li>简介：判断当前线程是否可被冻结，如果是，就冻结线程，之后再判断线程是否可以退出</li>
<li>返回值：{(boolean)是否可退出, (boolean)是否被冻结过}<ul>
<li>是否可退出<ul>
<li>true：可退出</li>
<li>false：不可退出</li>
</ul>
</li>
<li>是否被冻结过<ul>
<li>true：线程被冻结过</li>
<li>false：线程没有冻结过</li>
</ul>
</li>
</ul>
</li>
<li>示例：</li>
</ul>
<pre><code class="lua">-- Lua 线程循环的写法
repeat
  shldStop, froze = xwos.cthd.frzShldStop()
  threadLoop()
until (shldStop)
</code></pre>

<h4 id="xwoscthdfrz">xwos.cthd.frz()</h4>
<ul>
<li>简介：冻结当前线程</li>
</ul>
<h2 id="thdsp"><strong>类：thdsp</strong></h2>
<ul>
<li>说明：线程对象强指针类</li>
</ul>
<h4 id="thdspstop">thdsp:stop()</h4>
<ul>
<li>简介：终止子线程并等待它的返回值，最后回收子线程资源</li>
<li>参数thdsp：(userdata, 输入)类成员函数的this参数，子线程对象的强引用指针</li>
<li>返回值：{(number)函数的错误码, (number)子线程的返回值}，当函数的错误码为0时，
    子线程的返回值才有效</li>
<li>说明：<code>thdsp:stop()</code>等价于<code>thdsp:cancel() + thdsp:join()</code></li>
<li>示例：</li>
</ul>
<pre><code class="lua">rc, childrc = thdsp:stop()
</code></pre>

<h4 id="thdspcancel">thdsp:cancel()</h4>
<ul>
<li>简介：取消子线程并通知它退出</li>
<li>返回值：(number)函数的错误码</li>
<li>说明：<code>thdsp:cancel()</code>可中断子线程的阻塞态与睡眠态（互斥锁、
    信号量等的等待状态），并使得子线程调用<code>xwos.cthd.shldStop()</code>时获得
    返回值<code>true</code></li>
<li>示例：</li>
</ul>
<pre><code class="lua">rc = thdsp:cancel()
</code></pre>

<h4 id="thdspjoin">thdsp:join()</h4>
<ul>
<li>简介：等待线程结束并获取它的返回值，最后回收线程资源</li>
<li>参数thdsp：(userdata, 输入)类成员函数的this参数，子线程对象的强引用指针</li>
<li>返回值：{(number)函数的错误码, (number)子线程的返回值}，当函数的错误码为0时，
    子线程的返回值才有效</li>
<li>示例：</li>
</ul>
<pre><code class="lua">rc, childrc = thdsp:join()
</code></pre>

<h4 id="thdspdetach">thdsp:detach()</h4>
<ul>
<li>简介：分离线程</li>
<li>参数thdsp：(userdata, 输入)类成员函数的this参数，子线程对象的强引用指针</li>
<li>说明：<ul>
<li>此函数可将线程设置为<strong>DETACH</strong>态，处于分离态的线程退出后，系统自动回收资源，
    不需要父线程join()或stop()。</li>
<li>与<strong>DETACH</strong>相反的状态是<strong>JOINABLE</strong>，通过
    API <code>xwos.thd.dofile(file)</code>、<code>xwos.thd.dostring(str)</code>
    以及<code>xwos.thd.call(func)</code>创建的子线程都是<strong>JOINABLE</strong>态的。</li>
</ul>
</li>
</ul>
<h4 id="thdspmigratecpu">thdsp:migrate(cpu)</h4>
<ul>
<li>简介：将线程迁移到目标CPU</li>
<li>参数thdsp：(userdata, 输入)类成员函数的this参数，子线程对象的强引用指针</li>
<li>参数cpu：(number, 输入)目标CPU的ID</li>
<li>返回值：(number)函数的错误码</li>
</ul></div>
            </div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "../../..",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="../../../js/base.js" defer></script>
        <script src="../../../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="searchModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="searchModalLabel">搜索</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
                <p>
                    在这里所有文档：
                </p>
                <form>
                    <div class="form-group">
                        <input type="search" class="form-control" placeholder="搜索..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="keyboardModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="keyboardModalLabel">Keyboard Shortcuts</h4>
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>
